<?php
namespace app\userapi\controller;
use app\common\controller\Api;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

/**
 * 手机短信接口
 */
class Sms extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function sms($mobile,$code){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        AlibabaCloud::accessKeyClient('LTAIscedhILedfyx', 'XClEnQCLhx9I6Jcfcq9ASYXw5qYpJD')
            ->regionId('cn-hangzhou')
            ->asDefaultClient();
        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $mobile,
                        'SignName' => "亿家亿信息科技",
                        'TemplateCode' => "SMS_139241354",
                        'TemplateParam' => '{"code":'.$code.'}',
                    ],
                ])
                ->request();
            if($result->toArray()['Code'] == 'OK') {
                return 200;
            }else{
                return 100;
            }
        } catch (ClientException $e) {
            return $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
           return $e->getErrorMessage() . PHP_EOL;
        }
    }
}

